package com.xncoding.freemarker;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import cn.afterturn.easypoi.excel.entity.result.ExcelImportResult;
import com.alibaba.fastjson.JSON;
import com.xncoding.freemarker.model.*;
import com.xncoding.freemarker.service.GenerateCodeService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.File;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class poutest {

    @Autowired
    GenerateCodeService generateCodeService;

    @Test
    public void test(){
        //获取sheet数量
        int sheetNum = 1;
        ImportParams params = new ImportParams();
        //表头在第几行
//        params.setTitleRows(6);
        params.setTitleRows(8);
//        params.setTitleRows(10);
        //是否需要通过key-value导入方法，获取特定字段
        params.setReadSingleCell(true);
        //判断一个cell是key的规则,可以自定义,默认就是 “:”
        params.setKeyMark(":");

        List<mock> mCtrList = new ArrayList<>();
        mock moc = new mock();
        moc.setBusinessName("transferAccounts");
        moc.setBusinessNameNotes("转账汇款");

        List<mockCtr> mockCtrList = new ArrayList<>();
        mockCtr mocc = null;
        ColumnClass col ;
        jsonfile jsonf ;
        for (int sheetIndex = 0; sheetIndex < sheetNum; sheetIndex++) {
            //第几个sheet页
            params.setStartSheetIndex(sheetIndex);

            //获取表头下的数据
            ExcelImportResult<Map> result = ExcelImportUtil.importExcelMore(new File("C:\\Users\\weimo\\Desktop\\test\\5.xlsx"), Map.class, params);
            List<Map> list = result.getList();

            //获取特定字段的map
            Map<String, Object> map = result.getMap();
            System.out.println(JSON.toJSONString(list));
            System.out.println(JSON.toJSONString(map));
            System.out.println("----------------");

            List<ColumnClass> listColin = new ArrayList<>();
            List<jsonfile> listjsonin = new ArrayList<>();
//            List<ColumnClass> listColout = new ArrayList<>();
            mocc = new mockCtr();

            mocc.setModelName(map.get("服务方法:").toString());
            mocc.setEosPath(map.get("请求路径:").toString());
//            mocc.setEosPath(map.get("服务路径:").toString());
            mocc.setModelNameNotes(map.get("服务名称:").toString());

            for (Map mapMock : list){
                System.out.println(JSON.toJSONString(mapMock));
                if(!mapMock.containsKey("字段名称")|| null == mapMock.get("字段名称") || "字段名称".equals(mapMock.get("字段名称").toString())){
                    continue;
                }
                col = new ColumnClass();
                jsonf = new jsonfile();
                col.setPropertyName(getCOL("字段名称",mapMock));
                col.setType(getCOL("类型",mapMock));
                col.setPropertyNotes(getCOL("中文含义",mapMock));
                col.setRemark(getCOL("数据项说明",mapMock));
//                col.setLen(getCOL("长度",mapMock));
//                col.setColumnName(getCOL("excelRowNum",mapMock));
                col.setSfNoll(getCOL("是否必输",mapMock).equals("是")?true:false||getCOL("是否必输",mapMock).equals("Y")?true:false);
                if(col.getSfNoll()){
                    jsonf.setPropertyName(col.getPropertyName());
                    listjsonin.add(jsonf);
                }
                listColin.add(col);

            }
            mocc.setColumns(listColin);
            mocc.setJsonStr(listjsonin);
            System.out.println("============");
            System.out.println(JSON.toJSONString(listjsonin));
            mockCtrList.add(mocc);

        }
        moc.setMockcolumns(mockCtrList);
        mCtrList.add(moc);
        System.out.println(JSON.toJSONString(mCtrList));
        generateCodeService.mockGenerateCode(mCtrList, "C:\\Users\\weimo\\Desktop\\test\\");



        //--------------------------------------------------------------------------------------------------------
        List<mbank> classList = new ArrayList<>();
        mbank m = new mbank();
        m.setBusinessName("xxxx");
        m.setBusinessNameNotes("xxxx");
        m.setPackageName("com.xjrccb.bizplat.mbank.impl.fsl");


        controllerModel c1 = new controllerModel(mocc.getModelName(),mocc.getModelNameNotes(),mocc.getEosPath());

        List<controllerModel> list = new ArrayList<>();
        list.add(c1);

        m.setColumns(list);

        classList.add(m);
        generateCodeService.mbankGenerateCode(classList, "C:\\Users\\weimo\\Desktop\\test\\");


    }

    @Test
    public void test01(){
        List<mbank> classList = new ArrayList<>();
        mbank m = new mbank();
        m.setBusinessName("myAccount");
        m.setBusinessNameNotes("我的账户");
        m.setPackageName("com.xjrccb.bizplat.mbank.impl.asl");


        controllerModel c1 = new controllerModel("BdDvcGstrQry","查询个人手势开关","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcGstrQry");
        controllerModel c2 = new controllerModel("BdDvcGstrChk","校验个人手势密码","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcGstrChk");
        controllerModel c3 = new controllerModel("BdDvcGstrOpn","开启个人手势开关","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcGstrOpn");
        controllerModel c4 = new controllerModel("BdDvcGstrClo","关闭个人手势开关","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcGstrClo");

        controllerModel c5 = new controllerModel("BdDvcFgprtQry","查询个人指纹开关","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcFgprtQry");
        controllerModel c6 = new controllerModel("BdDvcFgprtOpn","开启个人指纹开关","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcFgprtOpn");
        controllerModel c7 = new controllerModel("BdDvcFgprtClo","关闭个人指纹开关","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcFgprtClo");
        controllerModel c8 = new controllerModel("BdDvcFaceQry","查询个人人脸开关","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcFaceQry");
        controllerModel c9 = new controllerModel("BdDvcFaceOpn","开启个人人脸开关","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcFaceOpn");
        controllerModel c10 = new controllerModel("BdDvcFaceClo","关闭个人人脸开关","/bcsec/bcsecp/FsBcsIdvBindDvc/BdDvcFaceClo");

        List<controllerModel> list = new ArrayList<>();
        list.add(c1);
        list.add(c2);
        list.add(c3);
        list.add(c4);
        list.add(c5);
        list.add(c6);
        list.add(c7);
        list.add(c8);
        list.add(c9);
        list.add(c10);

        m.setColumns(list);

        classList.add(m);
        generateCodeService.mbankGenerateCode(classList, "C:\\Users\\weimo\\Desktop\\test\\");
//        generateCodeService.mockGenerateCode(mCtrList, "D:\\Desktop\\test\\");
    }

    public static String getCOL(String key,Map map){
        System.out.println("====="+key);
        if(null != map.get(key) && !"".equals(map.get(key))){
            return map.get(key).toString().trim().replaceAll("\r\n ","");
        }

        return "";
    }
}
